var Bac = require("Bacteria");

cc.Class({
    extends: cc.Component,
 
    properties: {
        Bacteria:{
            default:null,
            type:Bac
        },
        accl:{
            default:10,
        }
    },
    // setInputControl:function(){
    //     var self = this;
    //     var listener= {
    //         event:cc.EventListener.KEYBOARD,
    //         onKeyPressed:function(keyCode,event){
    //             switch(keyCode){
    //                 case cc.macro.KEY.a:
    //                     self.accLeft= true;
    //                     break;
    //                 case cc.macro.KEY.d:
    //                     self.accRight= true;
    //                     break;
    //                 case cc.macro.KEY.w:
    //                     self.accUp= true;
    //                     break;
    //                 case cc.macro.KEY.s:
    //                     self.accDown= true;
    //                     break;
    //             }
    //         },
    //         onKeyReleased:function(keyCode,event){
    //             switch(keyCode){
    //                 case cc.macro.KEY.a:
    //                     self.accLeft= false;
    //                     break;
    //                 case cc.macro.KEY.d:
    //                     self.accRight= false;
    //                     break;
    //                 case cc.macro.KEY.w:
    //                     self.accUp= false;
    //                     break;
    //                 case cc.macro.KEY.s:
    //                     self.accDown= false;
    //                     break;
    //             }
    //         }
    //     }
    //     cc.eventManager.addListener(listener, self.node)
    // },
    onKeyDown (event) {
        switch(event.keyCode){                 
            case cc.macro.KEY.a:                   
                this.accLeft= true;
                //console.log("Press a");
                break;
            case cc.macro.KEY.d:                   
                this.accRight= true;      
                //console.log("Press d");                 
                break;                    
            case cc.macro.KEY.w:                    
                this.accUp= true;    
                //console.log("Press w");                    
                break;                    
            case cc.macro.KEY.s:                    
                this.accDown= true;  
                //console.log("Press s");                      
                break;                
        }
    },
    onKeyUp (event) {
        // unset a flag when key released
        switch(event.keyCode) {
            case cc.macro.KEY.a:
                this.accLeft= false;
                break;
            case cc.macro.KEY.d:
                this.accRight= false;
                break;
            case cc.macro.KEY.w:
                this.accUp= false;
                break;
            case cc.macro.KEY.s:
                this.accDown= false;
                break;
        }
    },
    // use this for initialization
    onLoad () {
        this.accLeft = false;
        this.accRight = false;
        this.accUp = false;
        this.accDoen = false;
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);   
    },
 
    // called every frame, uncomment this function to activate update callback
    update (dt) {
        if(this.accLeft){
            this.node.x -=this.accl;
        }
        if(this.accRight){
            this.node.x +=this.accl;
        }
        if(this.accUp){
            this.node.y +=this.accl;
        }
        if(this.accDown){
            this.node.y -=this.accl;
        }
     },
});